package com.usian.admin.listener;

import com.usian.admin.service.WemediaNewsAutoScanService;
import com.usian.model.common.constants.NewsAutoScanConstants;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class WeMediaKafkaListener {
    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @KafkaListener(topics = {NewsAutoScanConstants.WM_NEWS_AUTO_SCAN_TOPIC})
    private void listenNewsScan(ConsumerRecord<?,?>consumerRecord){
        Optional<? extends ConsumerRecord<?, ?>> recordOptional = Optional.ofNullable(consumerRecord);
        if (recordOptional.isPresent()){
            Integer newsId = (Integer) consumerRecord.value();
            wemediaNewsAutoScanService.autoScanByMediaNewsId(newsId);
        }
    }
}
